package com.librarydemo.demo.service.imp;

import com.librarydemo.demo.dao.UserDao;
import com.librarydemo.demo.entity.Book;
import com.librarydemo.demo.entity.BorrowRecord;
import com.librarydemo.demo.entity.User;
import com.librarydemo.demo.service.UserService;
import com.librarydemo.demo.util.MD5Tools;
import com.librarydemo.demo.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    public User loginCheck(String username, String password) {
        if(StringUtil.isEmpty(username) || StringUtil.isEmpty(password)){
            System.out.println("liu");
            return null;
        }
        return userDao.loginCheck(username, MD5Tools.MD5(password)) ;
    }

    @Override
    public boolean isNameExist(String username) {
        if(StringUtil.isEmpty(username)){
            return true;
        }
        int count = userDao.nameCheck(username) ;
        return count > 0 ? true : false ;
    }

    @Override
    public boolean register(User user) {
        if(user != null){
            int count = userDao.register(user.getUsername(), MD5Tools.MD5(user.getPassword()), user.getBorrowNumber(), user.getPhoneNumber(), user.getPicture());
            return count > 0 ? true : false;
        }
        return false ;
    }

    //更新头像
    @Override
    public boolean UpdatePic(int uid, String picture) {
        int count = userDao.updatePic(uid, picture) ;
        return count > 0 ? true : false;
    }

    @Override
    public User getById(int uid) {
        return userDao.getById(uid);
    }


    //更新密码
    @Override
    public boolean rePassword(int uid, String password) {
        if(StringUtil.isEmpty(password)){
            return false;
        }
        int count =  userDao.updatePassword(uid, MD5Tools.MD5(password)) ;
        return count > 0 ? true : false;
    }


    //获取用户头像
    @Override
    public String getPicture(int uid) {
        return userDao.getPicture(uid);
    }

    //借书
    @Transactional
    @Override
    public boolean borrowBook(int uid, int bid) {
        Calendar calendar = Calendar.getInstance();
        Date borrow_d = calendar.getTime();
        //归还日期为30天后
        calendar.add(Calendar.DATE, 30);
        Date return_d = calendar.getTime();


        Book book = userDao.checkBookBorrow(bid);
        if(userDao.checkUserBorrow(uid) > 0 && book.getRemain_num() > 0){
            //可以借阅
            userDao.updateBookRemain_num(bid);
            userDao.updateBorrowNumber(uid);
            userDao.addBorrowRecord(uid, bid, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(borrow_d), book.getName(), book.getAuthor(), book.getPublisher(), new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(return_d));

        }else {
            return false;
        }

        return true;
    }

    //还书
    @Transactional
    @Override
    public boolean returnBook(int uid, int bid) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BorrowRecord borrowRecord = userDao.getBorrowRecord(uid, bid);
        long nowDate = new Date().getTime();
        System.out.println(nowDate);
        try {
            long returnDate = simpleDateFormat.parse(borrowRecord.getReturnDate()).getTime();
            System.out.println(returnDate);
            if(returnDate < nowDate){
                //弹出支付界面,若支付成功刷新页面，完成还书功能，支付失败，抛出异常,返回false
            }else {
                //实现所有的还书功能
                userDao.deleteBorrowRecord(uid, bid);
                userDao.addOneBookRemain_num(bid);
                userDao.addOneBorrowNumber(uid);
                userDao.addReturnRecord(uid, bid, borrowRecord.getBname(), borrowRecord.getAuthor(), borrowRecord.getPublisher(), simpleDateFormat.format(new Date()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    @Override
    public List<BorrowRecord> getAllBorrowRecord(int uid) {
        return userDao.getAllBorrowRecord(uid);
    }

    @Override
    public List<Book> getBooks(String name, String author, String publisher, String catalog) {
        return userDao.getBooks(name, author, publisher, catalog);
    }
}
